<template>
  <header>
    <div class="container">
      <nav class="navbar">
        <div class="logo">
          <img src="@/assets/panda-logo.png" alt="熊猫社区">
          <h1>熊猫社区</h1>
        </div>
        
        <ul class="nav-links">
          <li><router-link to="/">首页</router-link></li>
          <li><router-link to="/community">社区</router-link></li>
          <li><router-link to="/database">资料库</router-link></li>
          <li><router-link to="/shop">商城</router-link></li>
        </ul>
        
        <div class="user-actions" v-if="isLoggedIn">
          <el-dropdown trigger="click">
            <img :src="userAvatar" alt="用户头像" class="user-avatar">
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="goToProfile">个人信息</el-dropdown-item>
                <el-dropdown-item @click="goToDocuments">我的文档</el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        <router-link to="/login" v-else class="login-btn">
          <el-button type="primary">登录/注册</el-button>
        </router-link>
      </nav>
    </div>
  </header>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

const router = useRouter();
const isLoggedIn = ref(false);
const userAvatar = ref('');

const checkLoginStatus = () => {
  const token = localStorage.getItem('token');
  if (token) {
    isLoggedIn.value = true;
    fetchUserInfo();
  }
};

const fetchUserInfo = async () => {
  try {
    const response = await fetch('/api/user/info', {
      headers: {
        'Authorization': `Bearer ${localStorage.getItem('token')}`
      }
    });
    if (response.ok) {
      const data = await response.json();
      userAvatar.value = data.avatar || '/images/default-avatar.jpg';
    }
  } catch (error) {
    console.error('获取用户信息失败:', error);
  }
};

const handleLogout = () => {
  localStorage.removeItem('token');
  isLoggedIn.value = false;
  ElMessage.success('退出登录成功');
  router.push('/login');
};

const goToProfile = () => {
  router.push('/user');
};

const goToDocuments = () => {
  router.push('/documents');
};

onMounted(() => {
  checkLoginStatus();
});
</script>

<style scoped>
header {
  background-color: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
}

.logo {
  display: flex;
  align-items: center;
}

.logo img {
  height: 40px;
  margin-right: 10px;
}

.logo h1 {
  font-size: 24px;
  color: var(--primary-color);
}

.nav-links {
  display: flex;
  list-style: none;
}

.nav-links li {
  margin-left: 30px;
}

.nav-links a {
  text-decoration: none;
  color: var(--primary-color);
  font-weight: bold;
  padding: 5px 0;
  position: relative;
  transition: color 0.3s;
}

.nav-links a:hover {
  color: var(--secondary-color);
}

.nav-links a.router-link-active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background-color: var(--secondary-color);
  border-radius: 3px;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  cursor: pointer;
  border: 2px solid var(--secondary-color);
}

.login-btn {
  text-decoration: none;
}
</style> 